<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="标签名称" prop="title">
      <el-input v-model="dataForm.title" placeholder="标签名称"></el-input>
    </el-form-item>
    <el-form-item label="标签颜色code(使用RGB表示法，如:#000)" prop="colorCode">
      <el-input v-model="dataForm.colorCode" placeholder="标签颜色code(使用RGB表示法，如:#000)"></el-input>
    </el-form-item>
    <el-form-item label="black" prop="codeTitle">
      <el-input v-model="dataForm.codeTitle" placeholder="black"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createdAt">
      <el-input v-model="dataForm.createdAt" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createdBy">
      <el-input v-model="dataForm.createdBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="modifiedAt">
      <el-input v-model="dataForm.modifiedAt" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="modifiedBy">
      <el-input v-model="dataForm.modifiedBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除【0：未删除， 1：已删除】" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="逻辑删除【0：未删除， 1：已删除】"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          title: '',
          colorCode: '',
          codeTitle: '',
          createdAt: '',
          createdBy: '',
          modifiedAt: '',
          modifiedBy: '',
          isDeleted: ''
        },
        dataRule: {
          title: [
            { required: true, message: '标签名称不能为空', trigger: 'blur' }
          ],
          colorCode: [
            { required: true, message: '标签颜色code(使用RGB表示法，如:#000)不能为空', trigger: 'blur' }
          ],
          codeTitle: [
            { required: true, message: 'black不能为空', trigger: 'blur' }
          ],
          createdAt: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createdBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          modifiedAt: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          modifiedBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '逻辑删除【0：未删除， 1：已删除】不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/film/filmlabel/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.title = data.filmLabel.title
                this.dataForm.colorCode = data.filmLabel.colorCode
                this.dataForm.codeTitle = data.filmLabel.codeTitle
                this.dataForm.createdAt = data.filmLabel.createdAt
                this.dataForm.createdBy = data.filmLabel.createdBy
                this.dataForm.modifiedAt = data.filmLabel.modifiedAt
                this.dataForm.modifiedBy = data.filmLabel.modifiedBy
                this.dataForm.isDeleted = data.filmLabel.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/film/filmlabel/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'title': this.dataForm.title,
                'colorCode': this.dataForm.colorCode,
                'codeTitle': this.dataForm.codeTitle,
                'createdAt': this.dataForm.createdAt,
                'createdBy': this.dataForm.createdBy,
                'modifiedAt': this.dataForm.modifiedAt,
                'modifiedBy': this.dataForm.modifiedBy,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
